package com.example.dllo.exe.fenlei.danpin;


import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dllo.exe.R;

import com.example.dllo.exe.bean.DanPinRightBean;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.main.BaseFragment;
import com.example.dllo.exe.fengzhuang.DBValue;


/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class DanPinFragment extends BaseFragment {
    private ListView danpinlistviewleft, danpinlistviewright;

    private int lastPosition;
    private int pos = -1;
    private String urlHou;


    @Override
    protected int setLayout() {
        return R.layout.danpinfragmentlayout;
    }

    @Override
    protected void initView() {
        danpinlistviewleft = bindView(R.id.danpinlistviewleft);
        danpinlistviewright = bindView(R.id.danpinlvRight);





    }

    @Override
    protected void initData() {
        danpinlistviewleft.setDivider(null);
        final DanPinLeftAdapter danPinLeftAdapter = new DanPinLeftAdapter(getContext());
        danpinlistviewleft.setAdapter(danPinLeftAdapter);
        NetTool netTool = new NetTool();
        netTool.getNetData(DBValue.FENLEI_DANPIN, DanPinRightBean.class, new NetTool.NetListener<DanPinRightBean>() {
            @Override
            public void onSuccess(DanPinRightBean danPinRightBean) {

                DanPinRightAdapter danpinRightAdapter = new DanPinRightAdapter(getContext());




                danpinlistviewright.setAdapter(danpinRightAdapter);

                danpinRightAdapter.setDanPinRightBean(danPinRightBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        danpinlistviewright.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                Log.d("DanPinFragment---i", "i:" + i);
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if(i!=pos) {
                    updateLeftListview(i);
                    danpinlistviewleft.setSelection(i);
                }
                pos = i;
                Log.d("DanPinFragment", "onScroll: ");

            }
        });
        //设置左边的listview 监听事件
        danpinlistviewleft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int firstVisibleItem = danpinlistviewright.getFirstVisiblePosition();
                updateLeftListview(i);
                danpinlistviewright.setSelection(Integer.parseInt(i + ""));

            }
        });



    }

    private void updateLeftListview(int firstVisibleItem) {
        TextView lasTextView = (TextView) danpinlistviewleft.findViewWithTag(lastPosition);
        Log.d("MainAty", "lasTextView:" + lasTextView);
        //在右边Listview第一次加载过程中会一直调用监听中的onscroll  这时的textView可能为空
        if (lasTextView != null) {
            lasTextView.setTextColor(Color.BLACK);//先将上一个textView字体的颜色改成黑色
            lasTextView.setBackgroundColor(0XFFF5F2F2);
        }
        TextView currenTextView = (TextView) danpinlistviewleft.findViewWithTag(firstVisibleItem);
        //再将当前要改变的TextView字体的颜色改成红色
        if (currenTextView != null) {
            currenTextView.setTextColor(Color.RED);
            currenTextView.setBackgroundColor(0XFFFFFFFF);
        }
        lastPosition = firstVisibleItem;

    }




}
